<?php 
class ControllerProductcustomertestimonial extends Controller {
	
	protected function addhttp($url) {
	    if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
	        $url = "http://" . $url;
	    }
	    return $url;
	}

	public function index() {  
    	$this->language->load('product/customertestimonial');
		
		$this->load->model('catalog/customertestimonial');

		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', '', 'SSL'),
      		'separator' => false
   		);

		
		$customertestimonial_total = $this->model_catalog_customertestimonial->getTotalcustomertestimonials();
			
		if ($customertestimonial_total) {

	  		$this->document->SetTitle ($this->language->get('heading_title'));

	   		$this->data['breadcrumbs'][] = array(
	       		'text'      => $this->language->get('heading_title'),
				'href'      => $this->url->link('product/customertestimonial', '', 'SSL'),
	      		'separator' => $this->language->get('text_separator')
	   		);

						
      		$this->data['heading_title'] = $this->language->get('heading_title');
      		$this->data['text_auteur'] = $this->language->get('text_auteur');
      		$this->data['button_continue'] = $this->language->get('button_continue');
      		$this->data['showall'] = $this->language->get('text_showall');
      		$this->data['write'] = $this->language->get('text_write');
      		$this->data['text_average'] = $this->language->get('text_average');
      		$this->data['text_stars'] = $this->language->get('text_stars');
      		$this->data['text_no_rating'] = $this->language->get('text_no_rating');
			
			$this->data['continue'] = $this->url->link('common/home', '', 'SSL');

			$this->page_limit = $this->config->get('config_catalog_limit');
			
			if (isset($this->request->get['page'])) {
				$page = $this->request->get['page'];
			} else { 
				$page = 1;
			}	

			$this->data['customertestimonials'] = array();
			
			if ( isset($this->request->get['customertestimonial_id']) ){
				$results = $this->model_catalog_customertestimonial->getcustomertestimonial($this->request->get['customertestimonial_id']);
			}
			else{
				$results = $this->model_catalog_customertestimonial->getcustomertestimonials(($page - 1) * $this->page_limit, $this->page_limit);
			}
			
			$this->load->model('tool/image');

			foreach ($results as $result) {
				
			$result['description'] = '<span>"</span> '.trim($result['description']).' <span>"</span>';
			$result['description'] = str_replace('<span>"</span><p>', '<span>"</span> ', $result['description']);
			$result['description'] = str_replace('</p><span>"</span>', ' <span>"</span>', $result['description']);


				if ($result['avatar']!="")
				{
					$avatar = $this->model_tool_image->resize($result['avatar'], 70, 70);	
				}
				else
				{
					$avatar = "";	
				}

				$this->data['customertestimonials'][] = array(
					'name'		=> $result['name'],
					'title'    		=> $result['title'],
					'rating'		=> $result['rating'],
					'description'	=> $result['description'],
					'avatar'		=> $avatar,
					'date_added'	=> date("m-d-Y", strtotime($result['date_added'])) //$result['date_added']



				);
			}
			
			$url = '';
	
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			
				$this->data['write_url'] = $this->url->link('product/sendcustomertestimonial', '', 'SSL'); 	
			
			if ( isset($this->request->get['customertestimonial_id']) ){
				$this->data['showall_url'] = $this->url->link('product/customertestimonial', '', 'SSL'); 	
			}
			else{
				$pagination = new Pagination();
				$pagination->total = $customertestimonial_total;
				$pagination->page = $page;
				$pagination->limit = $this->page_limit; 
				$pagination->text = $this->language->get('text_pagination');
				$pagination->url = $this->url->link('product/customertestimonial', '&page={page}', 'SSL');
				$this->data['pagination'] = $pagination->render();				

			}


			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/customertestimonial.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/product/customertestimonial.tpl';
			} else {
				$this->template = 'default/template/product/customertestimonial.tpl';
			}
			
			$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'
			);		
			
	  		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
    	} else {

				
	  		$this->document->SetTitle ( $this->language->get('text_error'));
			
      		$this->data['heading_title'] = $this->language->get('text_error');

      		$this->data['text_error'] = $this->language->get('text_error');

      		$this->data['button_continue'] = $this->language->get('button_continue');

	    		$this->data['continue'] = $this->url->link('common/home', '', 'SSL');

			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
			} else {
				$this->template = 'default/template/error/not_found.tpl';
			}
			
			$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'
			);
		
	  		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
    	}
  	}
}
?>